<template>
  <el-drawer v-model="visible" :title="!dataForm.id ? $t('add') : disabled ? $t('notice.view') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" :disabled="disabled" @keyup.enter="dataFormSubmitHandle()" label-width="100px">
      <el-form-item prop="content" label="考核内容：">
        <el-input v-model="dataForm.content" type="textarea" placeholder="请填写"></el-input>
      </el-form-item>
      <el-form-item prop="standardDescribe" label="标准描述：">
        <el-input v-model="dataForm.standardDescribe" type="textarea" placeholder="请填写"></el-input>
      </el-form-item>
      <el-form-item prop="isCount" label="是否计数：">
        <el-switch v-model="dataForm.isCount" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item prop="scoreRule" label="计分规则：">
        <el-input v-model="dataForm.scoreRule" placeholder="请填写"></el-input>
      </el-form-item>
      <!--      <el-form-item prop="isOneVeto" label="一票否决：">-->
      <!--        <el-switch v-model="dataForm.isOneVeto" :active-value="1" :inactive-value="0" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item prop="isChoose" label="手选分数：">-->
      <!--        <el-switch v-model="dataForm.isChoose" :active-value="1" :inactive-value="0" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item prop="chooseRange" label="选择范围：">-->
      <!--        <el-input v-model="dataForm.chooseRange" placeholder="请填写选择范围，-1,-2,-3..."></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item prop="isAccrual" label="是否累加：">-->
      <!--        <el-switch v-model="dataForm.isAccrual" :active-value="1" :inactive-value="0" />-->
      <!--      </el-form-item>-->
      <el-form-item prop="score" label="分数：">
        <el-input v-model="dataForm.score" placeholder="请填写分数" onkeyup="value=value.replace(/[^\-\d]/g,'')" min="0" />
      </el-form-item>
      <el-form-item prop="scoreTotal" label="总分上限：">
        <el-input v-model="dataForm.scoreTotal" placeholder="请填写总分上限" onkeyup="value=value.replace(/[^\-\d]/g,'')" min="0" />
      </el-form-item>
      <el-form-item prop="remark" label="备注：">
        <el-input v-model="dataForm.remark" placeholder="请填写" />
      </el-form-item>
    </el-form>
    <template v-if="!disabled" v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-drawer>
</template>

<script setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";

const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);
const disabled = ref(false);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  content: "",
  standardDescribe: "",
  id: "",
  isCount: "",
  scoreRule: "",
  // isOneVeto: 0,
  // isChoose: 0,
  // chooseRange: "",
  // isAccrual: 1,
  score: "",
  scoreTotal: "",
  remark: "",
  typeId: ""
});

const rules = ref({
  content: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  totalScore: [{ required: true, message: t("validate.required"), trigger: "blur" }]
});

const init = (typeId, id, dis) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.typeId = typeId;
  disabled.value = dis;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    dataForm.id = id;
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id) => {
  baseService.get(`/evaluation/clctypestandard/${id}`).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/evaluation/clctypestandard", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init,
  dataForm
});
</script>
